package com.liuhuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuhuan.Exception.ServiceException;
import com.liuhuan.entity.User;
import com.liuhuan.mapper.UserMapper;
import com.liuhuan.service.UserService;
import com.liuhuan.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author liuhuan
 * @Date 2024/7/4 19:05
 * @注释
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> login(LoginVo loginVo) {
        if (loginVo != null && loginVo.getUsername() != null && loginVo.getPassword() != null) {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("username", loginVo.getUsername())
                    .eq("password", loginVo.getPassword());
            List<User> users = userMapper.selectList(userQueryWrapper);
            if(users==null){
                //抛出异常
                throw new ServiceException("账号不存在");
            }
            return users;
        } else {
            return Collections.emptyList();
        }

    }

    @Override
    public Boolean register(User user) {
        Map<String,Object> map=new HashMap<>();
        map.put("username",user.getUsername());
        List<User> users = userMapper.selectByMap(map);
        if(users.isEmpty()){
            userMapper.insert(user);
            return true;

        }
        else{
           return false;
        }

    }
}
